/**
 * Project: springyounger.lottery.web
 * 
 * File Created at 2010-3-26
 * $Id$
 * 
 * Copyright 2009 Zjut Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Zjut Edu. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Zjut Edu.
 */
package com.springyouner.lottery.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.springyouner.lottery.tools.LotteryUtil;
import com.springyounger.lottery.dal.dataobject.LotteryPhase;
import com.springyounger.lottery.dal.dataobject.LotteryWinner;
import com.springyounger.lottery.service.user.PhaseService;

/**
 * 活动控制
 * 
 * @author springyounger
 */
public class PhaseController implements Controller {

	private static final Logger logger = LoggerFactory
			.getLogger(PhaseController.class);
	private PhaseService phaseService;

	@SuppressWarnings("unchecked")
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 当前日期
		Date today = LotteryUtil.getToday();
		String paraPhaseId = request.getParameter("phaseId");
		if (LotteryUtil.isEmpty(paraPhaseId)
				|| !LotteryUtil.isNumeric(paraPhaseId)) {
			if (logger.isDebugEnabled()) {
				logger.debug("活动id异常 {}", paraPhaseId);
			}
			return new ModelAndView("redirect:index.do");
		}
		int phaseId = Integer.parseInt(paraPhaseId);
		request.getSession().setAttribute("phaseId",
				request.getParameter("phaseId"));
		LotteryPhase lotteryPhase = phaseService.getPhaseById(phaseId);
		if (null == lotteryPhase) {
			logger.info("活动不存在!");
			return new ModelAndView("redirect:index.do");
		} else {
			Date gmtStart = lotteryPhase.getGmtStart();
			Date gmtEnd = lotteryPhase.getGmtEnd();
			// 不在抽奖日
			if (LotteryUtil.getQuot(today, gmtStart) > 0
					|| LotteryUtil.getQuot(today, gmtEnd) < 0) {
				logger.info("活动已经结束或者未开始,开始 {},结束 {}", LotteryUtil.datetime(
						gmtStart, LotteryUtil.DEFAULT_DATE_FORMAT), LotteryUtil
						.datetime(gmtEnd, LotteryUtil.DEFAULT_DATE_FORMAT));
				return new ModelAndView("redirect:index.do");
			}
			Map model = new HashMap();
			model.put("lotteryPhase", lotteryPhase);
			// 中奖名单显示
			List<LotteryWinner> winnerList = new ArrayList<LotteryWinner>();
			winnerList = phaseService.getLatestWinner(
					lotteryPhase.getPhaseId(), LotteryWinner.TOP_SIZE);
			model.put("winnerList", winnerList);
			return new ModelAndView("user/index", model);
		}
	}

	/**
	 * @param phaseService
	 *            the phaseService to set
	 */
	public void setPhaseService(PhaseService phaseService) {
		this.phaseService = phaseService;
	}
}
